package com.example.permission.myjbm.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.jbmsuppliers.R;


/**
 * 显示点赞和评论的popupwindow
 * 以后可以优化  可以考虑用线程写
 * Created by JBMhaier on 2017/2/21.
 */

public class MyPopupWindow implements View.OnClickListener{
    private  PopupWindow popupWindow;
    private View view;
    private LinearLayout lin;
    private int x;
    private boolean isCreate,isPlay=true;

    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {

          switch(msg.what){
              //创建popupWindow时进入
              case 0x111:
                  x+=10;
                  if(x>=0){
                      isPlay=false;
                      return;
                  }
                  lin.scrollTo(x,0);

                  break;
              //退出
              case 0x112:
                  x-=10;
                  if(x<=-lin.getMeasuredWidth()-5){
                      isPlay=true;
                      popupWindow.dismiss();
                      return;
                  }
                  lin.scrollTo(x,0);

                  break;

          }
            handler.sendEmptyMessageDelayed(msg.what,5);


        }
    };
    private static MyPopupWindow totalPopup;
    public static MyPopupWindow getInstance(Context c,DianZan dianzan){
        if(totalPopup==null){
            totalPopup=new MyPopupWindow(c,dianzan);
        }
        return totalPopup;

    }


    private MyPopupWindow(Context c, DianZan dianzan){
        popuOption=dianzan;
        initData(c);
        if(isCreate==false){
            popupWindow = new PopupWindow(view,ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            isCreate=true;
        }

    }

    private void initData(Context c) {
        view = View.inflate(c, R.layout.dsy_item_popup, null);
        TextView tv1 = (TextView) view.findViewById(R.id.textView);
        TextView tv2 = (TextView) view.findViewById(R.id.textView1);
        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        lin=(LinearLayout)view.findViewById(R.id.linear);
        lin.measure(0,0);
        x=-lin.getMeasuredWidth();
        lin.scrollTo(lin.getMeasuredWidth(),0);



    }

    public void show(View view1,int width,int height){
        popupWindow.showAsDropDown(view1,-width,-height+10);
        if(!isPlay){
            handler.sendEmptyMessageDelayed(0x112,10);
        }else{
            handler.sendEmptyMessageDelayed(0x111,10);
        }
    }

    public void hidden(){

        popupWindow.dismiss();
        handler.sendEmptyMessageDelayed(0x112,10);
        isPlay=true;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.textView:
                popuOption.dianZan();
                break;
            case R.id.textView1:
                popuOption.pingLun();
                break;
        }
        handler.sendEmptyMessageDelayed(0x112,10);
    }
private DianZan  popuOption;



    public interface DianZan{
        void pingLun();
        void dianZan();
    }

}
